﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;


public partial class View_ManageDrivers : System.Web.UI.Page
{
    private ManageDriverControl mgDriver;
    private string SESSION_SEARCH = "SEARCH";

    protected void Page_Load(object sender, EventArgs e)
    {
        mgDriver = new ManageDriverControl();
        if (!IsPostBack)
        {
            Session.Remove(SESSION_SEARCH);
        }
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(tbnExport);
    }

    protected void tbnExport_Click(object sender, EventArgs e)
    {
        rdGDriver.ExportSettings.Excel.Format = (GridExcelExportFormat)Enum.Parse(typeof(GridExcelExportFormat), "ExcelML");
        rdGDriver.ExportSettings.IgnorePaging = true;
        rdGDriver.ExportSettings.ExportOnlyData = true;
        rdGDriver.ExportSettings.OpenInNewWindow = true;
        rdGDriver.ExportSettings.FileName = "Danh-sach-lai-xe-" + DateTime.Now.ToString();
        rdGDriver.MasterTableView.ExportToExcel();
    }

    /// <summary>
    /// Su kien click tren button search
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string stringSearch = txtKeyWord.Text.Trim();
        if (txtKeyWord.Text.Trim() == String.Empty)
        {
            stringSearch = hdfSearchString.Value.Trim();
        }

        if (Session[SESSION_SEARCH] != null)
        {
            if (hdfSearchString.Value.Trim() != Session[SESSION_SEARCH].ToString())
            {
                stringSearch = hdfSearchString.Value.Trim();
            }
            if (Session[SESSION_SEARCH].ToString() != txtKeyWord.Text.Trim())
            {
                stringSearch = txtKeyWord.Text.Trim();
            }
        }

        hdfSearchString.Value = stringSearch;
        rdGDriver.DataSource = mgDriver.SearchDriversByKeyword(stringSearch);
        rdGDriver.Rebind();
        Session[SESSION_SEARCH] = stringSearch;
        txtKeyWord.Text = stringSearch;
    }

    /// <summary>
    /// Ham day du lieu vao radGrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if ((Session[SESSION_SEARCH] != null) && (Session[SESSION_SEARCH].ToString() != string.Empty))
        {
            rdGDriver.DataSource = mgDriver.SearchDriversByKeyword(Session[SESSION_SEARCH].ToString());
        }
        else
        {
            rdGDriver.DataSource = mgDriver.getAllDrivers();
        }
    }

    /// <summary>
    /// Ham insert du lieu vao radGrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Driver driver = new Driver();
        TextBox txtFullName = editedItem.FindControl("txtFullName") as TextBox;
        TextBox txtIDNumber = editedItem.FindControl("txtIDNumber") as TextBox;
        RadDatePicker DateOfBirth = editedItem.FindControl("BirthDatePicker") as RadDatePicker;
        TextBox txtRankDriver = editedItem.FindControl("txtRankDriver") as TextBox;
        TextBox txtNationality = editedItem.FindControl("txtNationality") as TextBox;
        RadDatePicker TimeOut = editedItem.FindControl("TimeOut") as RadDatePicker;
        TextBox txtResidence = editedItem.FindControl("txtResidence") as TextBox;
        TextBox txtPhone = editedItem.FindControl("txtPhone") as TextBox;
        TextBox txtEmail = editedItem.FindControl("txtEmail") as TextBox;
        driver.FullName = txtFullName.Text;
        int IDNumber;
        driver.IDNumber = int.TryParse(txtIDNumber.Text, out IDNumber) ? IDNumber : 0;
        DateTime temp;
        DateTime.TryParse(DateOfBirth.SelectedDate.ToString(), out temp);
        driver.DateOfBirth = temp;
        int RankDriver;
        driver.RankDriver = int.TryParse(txtRankDriver.Text, out RankDriver) ? RankDriver : 0;
        driver.Nationality = txtNationality.Text;
        DateTime timeout;
        DateTime.TryParse(TimeOut.SelectedDate.ToString(), out timeout);
        driver.TimeOut = timeout;
        driver.Residence = txtResidence.Text;
        driver.Phone = txtPhone.Text;
        driver.Email = txtEmail.Text;
        driver.AvaImage = "Empty";
        int _result = mgDriver.addNewDriver(driver);
        if (_result > 1)
            Message.Show(rapCountry, "Thêm dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapCountry, "Thêm dữ liệu không thành công1", Message.Type.Error);
        else
            Message.Show(rapCountry, "Thêm không thành công : Lỗi hệ thống", Message.Type.Error);
        rdGDriver.CurrentPageIndex = 0;
    }

    /// <summary>
    /// Ham delete 1 hang cua radgrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        int pageIndex = rdGDriver.CurrentPageIndex;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Driver driver = new Driver();
        int driverId;
        driver.ID = int.TryParse(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][DriverColumn.ID].ToString(), out driverId) ? driverId : 0;

        int _result = mgDriver.deleteDriver(driver);
        if (_result == 1)
            Message.Show(rapCountry, "Xóa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapCountry, "Xóa dữ liệu không thành công1", Message.Type.Error);
        else
            Message.Show(rapCountry, "Xóa không thành công : Lỗi hệ thống", Message.Type.Error);
        rdGDriver.CurrentPageIndex = pageIndex;
    }

    /// <summary>
    /// Kiem tra check luc day du lieu vao radGrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            Label lblDateOfBirth = (Label)item.FindControl("lblDateOfBirth");
            DateTime temp;
            DateTime.TryParse(lblDateOfBirth.Text, out temp);
            lblDateOfBirth.Text = String.Format("{0:dd/MM/yyyy}", temp);
            DateTime temp1;
            Label lblTimeOut = (Label)item.FindControl("lblTimeOut");
            DateTime.TryParse(lblTimeOut.Text, out temp1);
            lblTimeOut.Text = String.Format("{0:dd/MM/yyyy}", temp1);

            ImageButton Edit = ((ImageButton)item["Edit"].Controls[0]);
            Edit.CssClass = "btnEditDelete";
            Edit.Width = 20;
            Edit.Height = 20;
            Edit.Attributes.Add("style", "background-color:transparent");
            ImageButton Delete = ((ImageButton)item["Delete"].Controls[0]);
            Delete.CssClass = "btnEditDelete";
            Delete.Width = 20;
            Delete.Height = 20;
            Delete.Attributes.Add("style", "background-color:transparent;");
        }
        if (e.Item.IsInEditMode)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            Button btnAdd = new Button();
            btnAdd = item.FindControl("PerformInsertButton") as Button;
            if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
            {
                btnAdd.Text = "Thêm mới";
                btnAdd.CssClass = "myButton";
            }
            else
            {
                Button btnUpdate = new Button();
                btnUpdate = item.FindControl("UpdateButton") as Button;
                btnUpdate.Text = "Chỉnh sửa";
                btnUpdate.CssClass = "myButton";
            }
            Button btnCancel = new Button();
            btnCancel = item.FindControl("CancelButton") as Button;
            btnCancel.Text = "Hủy bỏ";
            btnCancel.CssClass = "myButton";
        }
    }

    protected void rdGDriver_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        int pageIndex = rdGDriver.CurrentPageIndex;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Driver driver = new Driver();
        int driverId;
        driver.ID = int.TryParse(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][DriverColumn.ID].ToString(), out driverId) ? driverId : 0;
        TextBox txtFullName = editedItem.FindControl("txtFullName") as TextBox;
        TextBox txtIDNumber = editedItem.FindControl("txtIDNumber") as TextBox;
        RadDatePicker DateOfBirth = editedItem.FindControl("BirthDatePicker") as RadDatePicker;
        TextBox txtRankDriver = editedItem.FindControl("txtRankDriver") as TextBox;
        TextBox txtNationality = editedItem.FindControl("txtNationality") as TextBox;
        RadDatePicker TimeOut = editedItem.FindControl("TimeOut") as RadDatePicker;
        TextBox txtResidence = editedItem.FindControl("txtResidence") as TextBox;
        TextBox txtPhone = editedItem.FindControl("txtPhone") as TextBox;
        TextBox txtEmail = editedItem.FindControl("txtEmail") as TextBox;
        driver.FullName = txtFullName.Text;
        int IDNumber;
        driver.IDNumber = int.TryParse(txtIDNumber.Text, out IDNumber) ? IDNumber : 0;
        DateTime temp;
        DateTime.TryParse(DateOfBirth.SelectedDate.ToString(), out temp);
        driver.DateOfBirth = temp;
        int RankDriver;
        driver.RankDriver = int.TryParse(txtRankDriver.Text, out RankDriver) ? RankDriver : 0;
        driver.Nationality = txtNationality.Text;
        DateTime timeout;
        DateTime.TryParse(TimeOut.SelectedDate.ToString(), out timeout);
        driver.TimeOut = timeout;
        driver.Residence = txtResidence.Text;
        driver.Phone = txtPhone.Text;
        driver.Email = txtEmail.Text;
        driver.AvaImage = "Empty";
        int _result = mgDriver.editDriver(driver);
        if (_result == 1)
            Message.Show(rapCountry, "Chỉnh sửa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapCountry, "Chỉnh sửa dữ liệu không thành công1", Message.Type.Error);
        else
            Message.Show(rapCountry, "Chỉnh sửa dữ liệu không thành công : Lỗi hệ thống", Message.Type.Error);
        rdGDriver.CurrentPageIndex = pageIndex;
    }

}